/**
 * @project sso-token-start V1.0
 * @Title:  TokenIdHandler.java  
 * @Package com.gitee.kinbug.sso.handler 
 * Copyright(c) 2020 KinBug Co. Ltd. 
 * All right reserved. 
 */
package com.gitee.kinbug.sso.handler;

import com.gitee.kinbug.sso.model.AuthUser;

/**   
 * Description:TODO(sso token id 生成起)   
 * @author: KinBug
 */
public class TokenIdHandler {


    /**
     * make client sessionId
     *
     * @param ssoUser ssoUser
     * @return String
     */
    public static String makeSessionId(AuthUser ssoUser){
        String sessionId = ssoUser.getUserid().concat("_").concat(ssoUser.getVersion());
        return sessionId;
    }

    /**
     * parse storeKey from sessionId
     *
     * @param sessionId sessionId
     * @return String
     */
    public static String parseStoreKey(String sessionId) {
        if (sessionId!=null && sessionId.indexOf("_")>-1) {
            String[] sessionIdArr = sessionId.split("_");
            if (sessionIdArr.length==2
                    && sessionIdArr[0]!=null
                    && sessionIdArr[0].trim().length()>0) {
                String userId = sessionIdArr[0].trim();
                return userId;
            }
        }
        return null;
    }

    /**
     * parse version from sessionId
     *
     * @param sessionId sessionId
     * @return String
     */
    public static String parseVersion(String sessionId) {
        if (sessionId!=null && sessionId.indexOf("_")>-1) {
            String[] sessionIdArr = sessionId.split("_");
            if (sessionIdArr.length==2
                    && sessionIdArr[1]!=null
                    && sessionIdArr[1].trim().length()>0) {
                String version = sessionIdArr[1].trim();
                return version;
            }
        }
        return null;
    }

}
